package com.wltt.healing.healinguserservice.model;

import lombok.Setter;

import java.io.Serializable;

@Setter
public class ResultData<T> implements Serializable {

    public static final Integer SUCCESS_CODE = 0;
    public static final String SUCCESS_MESSAGE = "操作成功";

    public static final Integer FAILURE_CODE = -1;
    public static final String FAILURE_MESSAGE = "操作失败";

    public static final Integer NO_PERMISSION_RESP_CODE = 4000;
    public static final String NO_PERMISSION_RESP_DESC = "无权限";

    /**
     * 返回状态码,0:成功，其他错误
     */
    private Integer code;

    /**
     * 返回的消息
     */
    private String message;

    /**
     * 数据
     */
    private T data;

    private ResultData() {
    }

    private ResultData(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    private ResultData(T data) {
        this.code = ResultData.SUCCESS_CODE;
        this.message = ResultData.SUCCESS_MESSAGE;
        this.data = data;
    }

    public static ResultData success() {
        return new ResultData(ResultData.SUCCESS_CODE, ResultData.SUCCESS_MESSAGE);
    }

    public static ResultData success(Object result) {
        return new ResultData(result);
    }

    public static ResultData failure() {
        return new ResultData(ResultData.FAILURE_CODE, ResultData.FAILURE_MESSAGE);
    }

    public static ResultData failure(String respDesc) {
        ResultData resultData = new ResultData();
        resultData.setCode(ResultData.FAILURE_CODE);
        resultData.setMessage(respDesc);
        return resultData;
    }

    public static ResultData failure(Integer respCode, String respDesc) {
        return new ResultData(respCode, respDesc);
    }

    public Integer getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public T getData() {
        return data;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("ResultData{");
        sb.append("code=").append(code);
        sb.append(", message='").append(message).append('\'');
        sb.append(", data=").append(data);
        sb.append('}');
        return sb.toString();
    }
}
